<?php

namespace app\controllers;

use app\models\OrderInfoGood;
use app\models\Task;
use app\models\User;
use app\models\Contract;
use app\tools\Re;
use app\tools\JPushHelper;
use app\tools\SMS;
use Yii;
set_time_limit(0);

//此控制器执行自动化脚本信息

class CrontabController extends BaseController {
    public $state_arr = ['待付款', '待发货', '待收货', '已完成', '已取消', '租赁中', '已评价'];
    //租赁商品到期推送
    public function actionGoodPush() {
        $order_good_list = OrderInfoGood::find()->where(['is_push' => 0])->andWhere(['<=', 'end_at', time()])->all();
        if ($order_good_list) {
            foreach ($order_good_list as $v) {
          		$extra = [
		        	'push_type' => 'spdq',
		        	'order_sn' => $v->order->order_sn,
                    'state' => $v->order->state,
                    'state_name' => $this->state_arr[$v->order->state],
		        	'push_user_type' => 1
		    	];
	    		JPushHelper::pushNotifyByReg((string)$v->order->user_id, '租赁商品到期通知', '您的租赁商品已到期，请至租赁详情查看', $extra);
                $v->is_push = 1;
                $v->save();
            }
        }
        return Re::data();
    }

    //新的抢修订单无人接单提醒
    public function actionRepairPush() {
        $pre_time = time() - 900;
        $task_list = Task::find()->where(['status' => 1, 'state' => 0, 'is_notice' => 0])->andWhere("created_at < '$pre_time'")->all();
        if ($task_list) {
          foreach ($task_list as $v) {
            $v->is_notice = 1;
            $v->save();
          }
        	$user_admin_id_arr = User::find()->where(['status' => 1, 'is_admin' => 1])->select("id")->column();
            $extra = [
	        	'push_type' => 'qxdd',
	        	'push_user_type' => 3
	    	  ];
          JPushHelper::pushNotifyByReg($user_admin_id_arr, '15分钟未接单通知', '新订单15分钟无人接单，请至后台查看', $extra);
        }
        return Re::data();
    }

    //合同到期提醒
    public function actionContractRemind() {
      $time = time() + 54000;
      $contract_list = Contract::find()->where(['status' => 1, 'is_send' => 0])->andWhere("end > 0 and end <= '$time'")->all();
      if ($contract_list) {
        foreach ($contract_list as $v) {
          if ($v->phones) {
            $phone_arr = explode(",", $v->phones);
            foreach ($phone_arr as $p) {
              SMS::send($p, $v->name, 'SMS_219750003');
            }
          }
          $v->is_send = 1;
          $v->save();
        }
      }
      return Re::data();
    }
}